<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            @click.stop="handleWorkshopApproval"
            v-permission="permissionList.workshopApprovalBtn"
            >车间票务专业审核通过</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleWorkshopReject"
            v-permission="permissionList.workshopRejectBtn"
            >车间票务专业驳回</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleDepartmentApproval"
            v-permission="permissionList.departmentApprovalBtn"
            >部门票务专业审核通过</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleDepartmentReject"
            v-permission="permissionList.departmentRejectBtn"
            >部门票务专业驳回</el-button
          >
          <el-button type="primary" v-if="permissionList.handleExportData" size="small" @click.stop="handleExportData"
            >汇总版导出</el-button
          >
          <el-button type="primary" v-if="permissionList.handleExportStation" size="small" @click.stop="handleExportStation"
          >按车站导出</el-button
          >
	  <el-button type="primary" v-if="permissionList.handleExportStation" size="small" @click.stop="handleExportStationAll"
          >按车站汇总导出</el-button
          >
          <el-button type="primary" v-if="permissionList.advancedDelete" size="small" @click.stop="advancedDelete"
          >高级删除</el-button
          >
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-view"
            :size="scope.size"
            :type="scope.type"
            @click.stop="handleView(scope.row)"
            >查看</el-button
          >
        </template>
      </avue-crud>
      <el-dialog
        width="50%"
        :title="dialogTitle"
        v-if="dialogVisible"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="dialogClosed"
      >
        <PettymoneyCheckView
          :reportData="reportData"
          :operation-type="operationType"
          @closed="dialogClosed"
        />
      </el-dialog>
      <el-dialog
        width="50%"
        title="导出"
        v-if="exportDialogVisible"
        :visible.sync="exportDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <avue-form ref="form" :option="exportOption" v-model="exportForm"></avue-form>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="handleExcelWrite">确认</el-button>
          <el-button size="small" @click="exportDialogVisible = false">取消</el-button>
        </el-row>
      </el-dialog>
      <el-dialog
        width="50%"
        title="导出"
        v-if="exportAllStationDialogVisible"
        :visible.sync="exportAllStationDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <avue-form ref="form" :option="exportOptionAllStation" v-model="exportAllStationForm"></avue-form>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="handleExcelWriteForAllStation">确认</el-button>
          <el-button size="small" @click="exportAllStationDialogVisible = false">取消</el-button>
        </el-row>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import PettymoneyCheckView from "./pettymoney-check-view.vue";
import baseCrud from "@/mixins/base-crud";
import {
  getObj,
  workshopApprovalObj,
  workshopRejectObj,
  departmentApprovalObj,
  departmentRejectObj,
  exportExcel,
  exportStation,
  exportExcelStationAll,
  removeById
} from "@/api/subwaydm/swstpettymoneycheck";
import { exportOption,exportOptionAllStation } from "@/const/crud/subwaydm/swstpettymoneycheck";
import { mapGetters } from "vuex";
import { deepClone, handleExcelResponse } from "@/util/util";

export default {
  name: "swstpettymoneycheck",
  mixins: [baseCrud("subwaydm/swstpettymoneycheck")],
  components: {
    PettymoneyCheckView,
  },
  data() {
    return {
      // 弹窗标题
      dialogTitle: "",
      // 操作类型
      operationType: "",
      // 盘点表数据
      reportData: {
        statisticsDate: "", // 盘点日期
        bookBalanceMoney: 0.0, // 账面结余金额(元)
        hundredNumber: 0, // 100元数量
        hundredMoney: 0.0, // 100元金额
        fiftiyNumber: 0, // 50元数量
        fiftiyMoney: 0.0, // 50元金额
        twentyNumber: 0, // 20元数量
        twentyMoney: 0.0, // 20元金额
        tenNumber: 0, // 10元数量
        tenMoney: 0.0, // 10元金额
        fiveNumber: 0, // 5元数量
        fiveMoney: 0.0, // 5元金额
        onePaperNumber: 0, // 1元纸币数量
        onePaperMoney: 0.0, // 1元纸币金额
        oneCoinNumber: 0, // 1元硬币数量
        oneCoinMoney: 0.0, // 1元硬币金额
        fivecentsPaperNumber: 0, // 5角纸币数量
        fivecentsPaperMoney: 0.0, // 5角纸币金额
        fivecentsCoinNumber: 0, // 5角硬币数量
        fivecentsCoinMoney: 0.0, // 5角硬币金额
        twocentsPaperNumber: 0, // 2角纸币数量
        twocentsPaperMoney: 0.0, // 2角纸币金额
        onecentsPaperNumber: 0, // 1角纸币数量
        onecentsPaperMoney: 0.0, // 1角纸币金额
        onecentsCoinNumber: 0, // 1角硬币数量
        onecentsCoinMoney: 0.0, // 1角硬币金额
        actualBookBalanceMoney: 0.0, // 实存现金合计
        diffBookBalanceMoney: 0.0, // 差异金额
        diffReason: "", // 差异原因
        checkPersonName: "", // 盘点人
        checkPersonJobNo: "", // 盘点人工号
        attendName: "", // 复核人
        attendJobNo: "", // 复核人工号
      },
      // 拷贝初始值
      resetReportData: {},
      // 详情弹窗
      dialogVisible: false,
      // 导出弹窗
      exportDialogVisible: false,

      //导出弹框--全部车站
      exportAllStationDialogVisible: false,

      // 导出表单项
      exportOption: exportOption,

      // 导出表单项--全部车站
      exportOptionAllStation: exportOptionAllStation,
      // 导出选择表单
      exportForm: {},
       // 导出选择表单
      exportAllStationForm: {}
    };
  },
  created() {
    this.resetReportData = deepClone(this.reportData);
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        handleExportData: this.vaildData(this.permissions.sw_st_pettymoney_check_handle_export_data, false),
        handleExportStation: this.vaildData(this.permissions.sw_st_pettymoney_check_handle_export_station, false),
        advancedDelete: this.vaildData(this.permissions.stm_swstpettymoneycheck_del, false),
        workshopApprovalBtn: this.vaildData(this.permissions.stm_swstpettymoneycheck_workshop_approval, false),
        workshopRejectBtn: this.vaildData(this.permissions.stm_swstpettymoneycheck_workshop_reject, false),
        departmentApprovalBtn: this.vaildData(this.permissions.stm_swstpettymoneycheck_department_approval, false),
        departmentRejectBtn: this.vaildData(this.permissions.stm_swstpettymoneycheck_department_reject, false)
      };
    },
  },
  methods: {
    handleView(row) {
      this.dialogTitle = "查看";
      this.operationType = "view";
      getObj(row.id).then((response) => {
        let res = response.data.data;
        Object.assign(this.reportData, res);
        this.dialogVisible = true;
      });
    },
    handleWorkshopApproval() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审核的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "3") {
          this.$message.error("请选择状态为【站长审核通过】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "5",
      };
      workshopApprovalObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    handleWorkshopReject() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要驳回的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "3" && item.checkStatus != "6") {
          this.$message.error("请选择状态为【站长审核通过】或【部门票务专业驳回】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "4",
      };
      workshopRejectObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    handleDepartmentApproval() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审核的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "5") {
          this.$message.error("请选择状态为【车间票务专业审核通过】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "7",
      };
      departmentApprovalObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    handleDepartmentReject() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要驳回的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "5") {
          this.$message.error("请选择状态为【车间票务专业审核通过】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "6",
      };
      departmentRejectObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    dialogClosed() {
      Object.assign(this.reportData, this.resetReportData);
      this.dialogVisible = false;
      this.getList(this.page);
    },
    //高级删除
    advancedDelete(){
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据！");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const deletePromises = ids.map(id => removeById(id));
        Promise.all(deletePromises)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });
    },
    // 导出
    async handleExportStation() {
      let tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length != 1) {
        this.$message.info("请选择一条备用金盘点数据！");
        return;
      }
      let ids = tableSelect.map((row) => row.id);
      let res = await exportStation(ids[0]);
      handleExcelResponse(res);
    },
    handleExportData() {
      this.exportDialogVisible = true;
    },
    //全部车站按日期导出
    handleExportStationAll() {
      this.exportAllStationDialogVisible = true;
    },
    // 导出
    async handleExcelWrite() {
      this.$refs.form.validate(async (valid, done, msg) => {
        if (valid) {
          let loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          })
          try {
            const res = await exportExcel(this.exportForm);
            handleExcelResponse(res);
          } finally {
            loading.close();
            this.exportDialogVisible = false;
            this.getList(this.page);
            this.$set(this.exportForm, 'dates1', null)
            this.$set(this.exportForm, 'dates2', null)
          }
          done()
        } else {
          console.log('error submit!!', msg);
          return false;
        }
      })
    },
      // 导出--多个车站多个Sheet
    async handleExcelWriteForAllStation() {
      this.$refs.form.validate(async (valid, done, msg) => {
        if (valid) {
          let loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          })
          try {
	    let checkTime = this.exportAllStationForm.checkTime;
            const res = await exportExcelStationAll(checkTime);
            handleExcelResponse(res);
          } finally {
            loading.close();
            this.exportAllStationDialogVisible = false;
            this.getList(this.page);
            this.$set(this.exportAllStationForm, 'checkTime', null)            
          }
          done()
        } else {
          console.log('error submit!!', msg);
          return false;
        }
      })
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
